package com.zxjbyte.yiyi;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.zxjbyte.yiyi.framework.mybatis.base.BasePO;
import com.zxjbyte.yiyi.framework.mybatis.mapper.BaseMapperX;
import com.zxjbyte.yiyi.framework.mybatis.service.BaseServiceX;
import com.zxjbyte.yiyi.framework.mybatis.service.impl.BaseServiceXImpl;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @Author zhangxingjia 
 * @Date 2025/3/19 15:46
 * @Version: 1.0
 */
public class MyBatisPlusGenerate {
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/yiyi-boot?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "Root123.";
    private static final String DAL_MODEL_PATH = "yiyi-modules\\yiyi-module-system";
    private static final String PACKAGE_PARENT = "com.zxjbyte.yiyi.module.system";
    private static final String PACKAGE_PARENT2 = PACKAGE_PARENT.replace(".", "\\");
    // 功能包名，例如：插件
    private static final String PACKAGE_FUNCTION = "plugin";

    public static void main(String[] args) {
        fastGenerate();
    }

    public static void fastGenerate() {
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    //作者
                    builder.author("zxjbyte")
                            //输出路径(写到java目录)
                            .outputDir(System.getProperty("user.dir") + "\\" + DAL_MODEL_PATH + "\\src\\main\\java")
                            //开启swagger
//                            .enableSwagger()
                            .commentDate("yyyy-MM-dd")
                            .disableOpenDir();

                })
                .packageConfig(builder -> {
                    builder.parent(PACKAGE_PARENT)
//                            .moduleName(scanner("*****模块名*****"))
                            .entity("domain.entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller." + PACKAGE_FUNCTION)
                            .mapper("mapper")
                            .xml("xml")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "\\" + DAL_MODEL_PATH + "\\src\\main\\java\\" + PACKAGE_PARENT2 + "\\mapper\\xml"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(scanner("*****表名*****"))
                            .addTablePrefix("sys_") //设置过滤表前缀

                            .serviceBuilder()
                            .enableFileOverride() // 开启覆盖之前生成的文件
                            .superServiceClass(BaseServiceX.class)
                            .superServiceImplClass(BaseServiceXImpl.class)
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")

                            .entityBuilder()
                            .enableFileOverride()
                            .enableLombok()
                            .superClass(BasePO.class)
                            .logicDeleteColumnName("deleted")
                            .formatFileName("%sPO")
                            .enableTableFieldAnnotation()

                            .controllerBuilder()
                            .enableFileOverride()
                            .formatFileName("%sController")
                            .enableRestStyle()

                            .mapperBuilder()
                            .enableFileOverride()
                            //生成通用的resultMap
                            .enableBaseResultMap()
                            .superClass(BaseMapperX.class)
                            .formatMapperFileName("%sMapper")
                            .mapperAnnotation(Mapper.class)
                            .formatXmlFileName("%sMapper");
                })
                //忽略生成模块
                .templateConfig(builder -> {
                    builder.disable(TemplateType.CONTROLLER)
//                            .disable(TemplateType.SERVICE)
//                            .disable(TemplateType.SERVICEIMPL)
                    ;
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    public static String scanner(String trip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入：" + trip + ":");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + trip + "！");
    }

}
